New Ticket Alerts to Teams

工作流概述

这是一个包含8个节点的中等工作流,主要用于自动化处理各种任务。

工作流源代码

下载
{
  "id": "0H2mo5k35e0nzMEE",
  "meta": {
    "instanceId": "2e2d423885cf86d4b5420a96c93cd261c847d0419e9bb242fa12caf4a4c298c3",
    "templateCredsSetupCompleted": true
  },
  "name": "New Ticket Alerts to Teams",
  "tags": [],
  "nodes": [
    {
      "id": "80c29a2a-c005-4a19-a71e-3e862a4f9b49",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -120,
        540
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/1 8-16 * * 1-5"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "24b7e81c-51ea-4a0f-9684-e5aef53021ad",
      "name": "Add Filterable Parameter",
      "type": "n8n-nodes-base.code",
      "position": [
        460,
        460
      ],
      "parameters": {
        "jsCode": "for (const item of $input.all()) {
  // Assuming 'id' is the field with the Connectwise Ticket ID
  // Convert 'id' to a string to ensure it has quotes in the JSON output
  item.json.id = item.json.id.toString();

  // If 'filterOnThis' is another field you want to set with the id as a string
  item.json.FilterOnThis = item.json.id;

  // ... any other operations you want to perform on each item
}

return $input.all();"
      },
      "typeVersion": 2
    },
    {
      "id": "1ab5a549-34a2-4bed-9c4c-9c268bf04e0d",
      "name": "Query Database",
      "type": "n8n-nodes-base.redis",
      "position": [
        460,
        620
      ],
      "parameters": {
        "key": "={{ $json.id.toString() }}",
        "keyType": "string",
        "options": {},
        "operation": "get",
        "propertyName": "=Tickets"
      },
      "credentials": {
        "redis": {
          "id": "nm82iTY9aRTp8ZQm",
          "name": "Redis-Dispatch"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "c6f3bb14-3385-4b5a-95b1-f0ac787d056a",
      "name": "Filter Out Tickets that have already been sent",
      "type": "n8n-nodes-base.merge",
      "position": [
        780,
        540
      ],
      "parameters": {
        "mode": "combine",
        "options": {
          "fuzzyCompare": true
        },
        "joinMode": "keepNonMatches",
        "mergeByFields": {
          "values": [
            {
              "field1": "FilterOnThis",
              "field2": "Tickets"
            }
          ]
        },
        "outputDataFrom": "input1"
      },
      "typeVersion": 2.1
    },
    {
      "id": "18bb4e45-cfaf-47b7-88fa-4edb316f05d5",
      "name": "Get New Tickets",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        180,
        540
      ],
      "parameters": {
        "url": "https://na.myconnectwise.net/v4_6_release/apis/3.0/service/tickets?conditions=(status/name=\"New\" or status/name=\"New (email)\" or status/name=\"New (portal)\") and (board/id=25 or board/id=26 or board/id=1 or board/id=28) and parentTicketId=null&PageSize=999",
        "options": {},
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "clientId",
              "value": "934a9a6d-480a-4502-ab77-46bd80b368d7"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "MlbbiZdsGxeWRyMH",
          "name": "Header Auth account"
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "5a827e46-b257-4078-ba1f-a27bfba7cb02",
      "name": "Combine like Companies",
      "type": "n8n-nodes-base.code",
      "position": [
        1040,
        620
      ],
      "parameters": {
        "jsCode": "// would need to be adapted to your specific data structure.
return Object.values(items.reduce((accumulator, current) => {
  const siteName = current.json.siteName; // assuming 'siteName' is the common property
  const companyName = current.json.company; // replace with the correct path to the company name
  const ticketType = current.json.recordType; // replace with the correct path to the ticket type

  // Use a combined key of siteName and companyName to group tickets
  const groupKey = `${siteName} - ${companyName}`;

  if (!accumulator[groupKey]) {
    accumulator[groupKey] = {
      siteName,
      companyName,
      ticketType,
      tickets: []
    };
  }

  // Create a string that combines the ticket number and summary with a <br> for HTML line breaks
  const ticketInfo = `${current.json.id}: ${current.json.summary}<br>`;
  accumulator[groupKey].tickets.push(ticketInfo);

  // If ticketType is not consistent within the same groupKey, handle accordingly
  if (!accumulator[groupKey].ticketType) {
    accumulator[groupKey].ticketType = ticketType;
  } else if (accumulator[groupKey].ticketType !== ticketType) {
    // Handle the case where different ticket types exist within the same groupKey
    accumulator[groupKey].ticketType += `, ${ticketType}`;
  }

  return accumulator;
}, {})).map(group => {
  // Join the tickets array into a single string, separating each ticket with an empty string (effectively nothing)
  const ticketsString = group.tickets.join('');

  // Return the final object structure, with each property as needed
  return {
    siteName: group.siteName,
    companyName: group.companyName,
    ticketType: group.ticketType,
    tickets: ticketsString // This is now a single string with <br> as separators
  };
});
"
      },
      "typeVersion": 2
    },
    {
      "id": "0a69f405-cb56-4cb5-b56c-9015602376eb",
      "name": "Teams to Dispatch",
      "type": "n8n-nodes-base.microsoftTeams",
      "position": [
        1320,
        540
      ],
      "parameters": {
        "chatId": "19:3a9ec7df-5b99-4311-9a78-61ac2192da07_449d57c9-64d0-496f-ad07-147a6b388a32@unq.gbl.spaces",
        "message": "=Hey Dispatch Team!, A new {{ $json.ticketType }} has come in.<br><br> <strong>Ticket:</strong> {{ $json.tickets }} <strong>Company: </strong> {{ $json.companyName.name }}",
        "options": {},
        "resource": "chatMessage",
        "messageType": "html"
      },
      "credentials": {
        "microsoftTeamsOAuth2Api": {
          "id": "9eUxYgQYNgePrgUD",
          "name": "Microsoft Teams account"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "59beaef0-77af-4ae2-a68d-43313e933a10",
      "name": "Log in Redis",
      "type": "n8n-nodes-base.redis",
      "position": [
        1040,
        460
      ],
      "parameters": {
        "key": "={{ $json.id }}",
        "value": "={{ $json.id }}",
        "operation": "set"
      },
      "credentials": {
        "redis": {
          "id": "nm82iTY9aRTp8ZQm",
          "name": "Redis-Dispatch"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "ab7ae9df-5adf-4be4-8c56-39b433641673",
  "connections": {
    "Query Database": {
      "main": [
        [
          {
            "node": "Filter Out Tickets that have already been sent",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Get New Tickets": {
      "main": [
        [
          {
            "node": "Query Database",
            "type": "main",
            "index": 0
          },
          {
            "node": "Add Filterable Parameter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get New Tickets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine like Companies": {
      "main": [
        [
          {
            "node": "Teams to Dispatch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Filterable Parameter": {
      "main": [
        [
          {
            "node": "Filter Out Tickets that have already been sent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Out Tickets that have already been sent": {
      "main": [
        [
          {
            "node": "Combine like Companies",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log in Redis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

功能特点

  • 自动检测新邮件
  • AI智能内容分析
  • 自定义分类规则
  • 批量处理能力
  • 详细的处理日志

技术分析

节点类型及作用

  • Scheduletrigger
  • Code
  • Redis
  • Merge
  • Httprequest

复杂度评估

配置难度:
★★★☆☆
维护难度:
★★☆☆☆
扩展性:
★★★★☆

实施指南

前置条件

  • 有效的Gmail账户
  • n8n平台访问权限
  • Google API凭证
  • AI分类服务订阅

配置步骤

  1. 在n8n中导入工作流JSON文件
  2. 配置Gmail节点的认证信息
  3. 设置AI分类器的API密钥
  4. 自定义分类规则和标签映射
  5. 测试工作流执行
  6. 配置定时触发器(可选)

关键参数

参数名称 默认值 说明
maxEmails 50 单次处理的最大邮件数量
confidenceThreshold 0.8 分类置信度阈值
autoLabel true 是否自动添加标签

最佳实践

优化建议

  • 定期更新AI分类模型以提高准确性
  • 根据邮件量调整处理批次大小
  • 设置合理的分类置信度阈值
  • 定期清理过期的分类规则

安全注意事项

  • 妥善保管API密钥和认证信息
  • 限制工作流的访问权限
  • 定期审查处理日志
  • 启用双因素认证保护Gmail账户

性能优化

  • 使用增量处理减少重复工作
  • 缓存频繁访问的数据
  • 并行处理多个邮件分类任务
  • 监控系统资源使用情况

故障排除

常见问题

邮件未被正确分类

检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。

Gmail认证失败

确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。

调试技巧

  • 启用详细日志记录查看每个步骤的执行情况
  • 使用测试邮件验证分类逻辑
  • 检查网络连接和API服务状态
  • 逐步执行工作流定位问题节点

错误处理

工作流包含以下错误处理机制:

  • 网络超时自动重试(最多3次)
  • API错误记录和告警
  • 处理失败邮件的隔离机制
  • 异常情况下的回滚操作